package com.parktech.sun.web.controller.management.report;

import com.parktech.sun.domain.model.report.KeyEconomyPropFinishStatus;
import com.parktech.sun.domain.model.report.KeyTechnicRDStatus;
import com.parktech.sun.domain.model.report.KeyTechnicReformInvestStatus;
import com.parktech.sun.domain.service.report.KeyEconomyPropFinishStatusService;
import com.parktech.sun.domain.service.report.KeyTechnicRDStatusService;
import com.parktech.sun.domain.service.report.KeyTechnicReformInvestStatusService;
import com.parktech.sun.domain.service.report.NewEnergyEnterprisePropReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: guo
 * Date: 11-1-16
 * Time: 下午7:33
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/management/report")
public class NewEnergyEnterprisePropReportController {
    @Autowired
    private NewEnergyEnterprisePropReportService newEnergyEnterprisePropReportService;
    @Autowired
    private KeyTechnicRDStatusService keyTechnicRDStatusService;
    @Autowired
    private KeyEconomyPropFinishStatusService keyEconomyPropFinishStatusService;
    @Autowired
    private KeyTechnicReformInvestStatusService keyTechnicReformInvestStatusService;
/*    @Autowired
    private EnterpriseService enterpriseService;*/

    //   private final String redirectListURL = "redirect:listNewEnergyEnterprisePropReports";

    @RequestMapping(value = "/generateNewEnergyEnterprisePropReport", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView generateNewEnergyEnterprisePropReport(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("management/report/mainReport");
        List fieldColumns = new ArrayList();      // @todo:add the datetime condition
        List searchValues = new ArrayList();     // @todo:add the datetime condition
        String[] sortSchemas = {"DESC"};
        String[] sortKeys = {"enterprise.id"};
        List<KeyEconomyPropFinishStatus> keyEconomyPropFinishStatusList = keyEconomyPropFinishStatusService.getPaginationObjectsWithSimilarCondition(KeyEconomyPropFinishStatus.class, 1, 9999, null, null, sortSchemas, sortKeys);
        Map<Long,KeyEconomyPropFinishStatus> enterpriseID2KeyEPFSMap = new HashMap<Long,KeyEconomyPropFinishStatus>();
        Map<Long,Integer> enterpriseID2RowSpanNumMap = new HashMap<Long,Integer>();
        //Map<Long, List<KeyEconomyPropFinishStatus>> enterpriseID2KeyEPFSListMap = new HashMap<Long, List<KeyEconomyPropFinishStatus>>();
        Map<Long, List<KeyTechnicRDStatus>> enterpriseID2KeyTRDSListMap = new HashMap<Long, List<KeyTechnicRDStatus>>();
        Map<Long, List<KeyTechnicReformInvestStatus>> enterpriseID2KeyTRISListMap = new HashMap<Long, List<KeyTechnicReformInvestStatus>>();
        Map<Long,String> enterpriseID2NameMap = new HashMap<Long,String>();
        for (KeyEconomyPropFinishStatus keyEconomyPropFinishStatus : keyEconomyPropFinishStatusList) {
            enterpriseID2NameMap.put(keyEconomyPropFinishStatus.getEnterprise().getId(),keyEconomyPropFinishStatus.getEnterprise().getName());
            enterpriseID2KeyEPFSMap.put(keyEconomyPropFinishStatus.getEnterprise().getId(),keyEconomyPropFinishStatus);
            enterpriseID2RowSpanNumMap.put(keyEconomyPropFinishStatus.getEnterprise().getId(),keyEconomyPropFinishStatus.getKeyEconomyPropOutputList().size());
            /*if (!enterpriseID2KeyEPFSListMap.containsKey(keyEconomyPropFinishStatus.getEnterprise().getId())) {
                List<KeyEconomyPropFinishStatus> keyEconomyPropertyFinishStatusList = new ArrayList<KeyEconomyPropFinishStatus>();
                keyEconomyPropertyFinishStatusList.add(keyEconomyPropFinishStatus);
                enterpriseID2KeyEPFSListMap.put(keyEconomyPropFinishStatus.getEnterprise().getId(), keyEconomyPropertyFinishStatusList);
            } else {
                List<KeyEconomyPropFinishStatus> keyEconomyPropertyFinishStatusList = enterpriseID2KeyEPFSListMap.get(keyEconomyPropFinishStatus.getEnterprise().getId());
                if (!keyEconomyPropertyFinishStatusList.contains(keyEconomyPropFinishStatus)) {
                    keyEconomyPropertyFinishStatusList.add(keyEconomyPropFinishStatus);
                    enterpriseID2KeyEPFSListMap.put(keyEconomyPropFinishStatus.getEnterprise().getId(), keyEconomyPropertyFinishStatusList);
                }
            }*/

            if (!enterpriseID2KeyTRDSListMap.containsKey(keyEconomyPropFinishStatus.getEnterprise().getId())) {
                fieldColumns.clear();
                searchValues.clear();
                fieldColumns.add("enterprise.id");
                searchValues.add(keyEconomyPropFinishStatus.getEnterprise().getId());
                List<KeyTechnicRDStatus> keyTechnicRDStatusList = keyTechnicRDStatusService.getPaginationObjectsWithSimilarCondition(KeyTechnicRDStatus.class, 1, 9999, fieldColumns, searchValues, null, null);
                enterpriseID2KeyTRDSListMap.put(keyEconomyPropFinishStatus.getEnterprise().getId(), keyTechnicRDStatusList);
                if(enterpriseID2RowSpanNumMap.get(keyEconomyPropFinishStatus.getEnterprise().getId())<keyTechnicRDStatusList.size())
                {
                    enterpriseID2RowSpanNumMap.put(keyEconomyPropFinishStatus.getEnterprise().getId(),keyTechnicRDStatusList.size());
                }
            }
            if (!enterpriseID2KeyTRISListMap.containsKey(keyEconomyPropFinishStatus.getEnterprise().getId())) {
                fieldColumns.clear();
                searchValues.clear();
                fieldColumns.add("enterprise.id");
                searchValues.add(keyEconomyPropFinishStatus.getEnterprise().getId());
                List<KeyTechnicReformInvestStatus> keyTechnicReformInvestStatusList = keyTechnicReformInvestStatusService.getPaginationObjectsWithSimilarCondition(KeyTechnicReformInvestStatus.class, 1, 9999, fieldColumns, searchValues, null, null);
                enterpriseID2KeyTRISListMap.put(keyEconomyPropFinishStatus.getEnterprise().getId(), keyTechnicReformInvestStatusList);
                if(enterpriseID2RowSpanNumMap.get(keyEconomyPropFinishStatus.getEnterprise().getId())<keyTechnicReformInvestStatusList.size())
                {
                    enterpriseID2RowSpanNumMap.put(keyEconomyPropFinishStatus.getEnterprise().getId(),keyTechnicReformInvestStatusList.size());
                }
            }
        }
        return modelAndView.addObject("enterpriseID2KeyEPFSMap",enterpriseID2KeyEPFSMap).addObject("enterpriseID2RowSpanNumMap",enterpriseID2RowSpanNumMap).addObject("enterpriseID2NameMap",enterpriseID2NameMap).addObject("enterpriseID2KeyTRDSListMap", enterpriseID2KeyTRDSListMap).addObject("enterpriseID2KeyTRISListMap", enterpriseID2KeyTRISListMap);
    }

}
    

